package com.cntv.viewmodeldemo.livedata;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

/**
 * Created By Mahongyin
 * Date    2020/9/17 14:28
 * 用Livedata 包装  以getValue()  setValue() 形式操作实际数据
 * MutableLiveData:开放了setValue() 和 postValue()方法
 */
public class MyLiveDataViewModel extends ViewModel {

    private MutableLiveData<Integer> number;

    public MutableLiveData<Integer> getNum() {
        if (null== number ) {
            number = new MutableLiveData<Integer>();
            number.setValue(0);
        }
        return number;
    }

    public void add(int n) {
        number.setValue(number.getValue()==null?0:number.getValue()+n);
    }
}
